package cn.ustb.controller;

import cn.ustb.entity.CourseSchedule;
import cn.ustb.entity.CourseScheduleShow;
import cn.ustb.entity.User;
import cn.ustb.service.CourseScheduleService;
import cn.ustb.serviceImpl.CourseScheduleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yuyan
 */
@WebServlet("/courseScheduleServlet.do")
public class CourseScheduleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession();
        String yearTerm = request.getParameter("yearTerm");
        String week = request.getParameter("week");
        User user = (User) session.getAttribute("user");
        if(!"".equals(yearTerm) && !"".equals(week)){
            CourseScheduleService courseScheduleService=new CourseScheduleServiceImpl();
            List<CourseSchedule> courseSchedules = courseScheduleService.getCourseSchedule(user.getUsername(), yearTerm, week);

            List<CourseScheduleShow> courseScheduleShows = getCourseScheduleShow(courseSchedules);
            System.out.println("courseSchedules:");
            System.out.println(courseScheduleShows);
            session.setAttribute("courseScheduleShows", courseScheduleShows);
        }
        request.getRequestDispatcher("/courseSchedule.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    private List<CourseScheduleShow> getCourseScheduleShow(
            List<CourseSchedule> courseSchedules) {
        List<CourseScheduleShow> courseScheduleShows = new ArrayList<>();


        String[] joints = {"1-2","3-4","5","6-7","8-9","10-12"};
        String[] weekdays = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};


        if(courseSchedules != null){
            for(int i = 0;i < 6;i++){
                CourseSchedule[] weekday = new CourseSchedule[7];
                for(CourseSchedule courseSchedule : courseSchedules){
                    if(courseSchedule.getJoint().equals(joints[i])){
                        for(int j = 0;j < 7;j++){
                            if(courseSchedule.getWeekday().equals(weekdays[j])){
                                weekday[j] = courseSchedule;
                            }
                        }
                    }
                }
                CourseScheduleShow courseScheduleShow = new CourseScheduleShow(joints[i],
                        weekday[0], weekday[1], weekday[2], weekday[3], weekday[4], weekday[5], weekday[6]);
                courseScheduleShows.add(courseScheduleShow);
            }
        }

        return courseScheduleShows;
    }

}
